﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyPush
{
    public partial class ExtrasForm : Form
    {
        public Dictionary<string, object> Extras
        {
            get
            {
                Dictionary<string, object> _extras = new Dictionary<string, object>();
                foreach (ExtraInfo extra in this._Extras)
                {
                    _extras.Add(extra.Key, extra.Value);
                }
                return _extras;
            }
        }
        private List<ExtraInfo> _Extras;
        public ExtrasForm(Dictionary<string, object> extras)
        {
            InitializeComponent();
            this._Extras = new List<ExtraInfo>();
            if (extras != null)
            {
                foreach (KeyValuePair<string, object> extra in extras)
                {
                    this._Extras.Add(new ExtraInfo() { Key = extra.Key, Value = extra.Value });
                }
            }
        }

        private void ExtrasForm_Load(object sender, EventArgs e)
        {
            this.listBoxMain.Items.Clear();
            foreach (ExtraInfo extra in this._Extras)
            {
                this.listBoxMain.Items.Add(extra);
            }
            this.comboBoxType.SelectedIndex = 0;//0-string,1-int,2-bool
            this.comboBoxBool.SelectedIndex = 0;//0-true,1-false
        }

        private void fnChangeToEdit(int index)
        {
            tValue.Visible = index == 0;
            comboBoxBool.Visible = index == 2;
            tNumber.Visible = index == 1;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string sKey = tKey.Text;
                if (string.IsNullOrEmpty(sKey))
                    throw new Exception("Key不允许为空！");
                ExtraInfo info = new ExtraInfo() { Key = sKey };
                if (_Extras.Exists((et) => { return et.Key == sKey; }))
                    throw new Exception("已存在相同的Key！");
                switch (this.comboBoxType.SelectedIndex)
                {
                    case 0:
                        info.Value = tValue.Text;
                        break;
                    case 1:
                        info.Value = Convert.ToInt32(tNumber.Text);
                        break;
                    case 2:
                        info.Value = this.comboBoxBool.SelectedIndex == 0;
                        break;
                }
                this.listBoxMain.Items.Add(info);
                this._Extras.Add(info);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            fnChangeToEdit(comboBoxType.SelectedIndex);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void menuBtnDelete_Click(object sender, EventArgs e)
        {
            ExtraInfo info = listBoxMain.SelectedItem as ExtraInfo;
            if (info != null)
            {
                listBoxMain.Items.Remove(info);
                this._Extras.Remove(info);
            }
        }
    }
}
